﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Weazel.Willows.Model
{
	public partial class QueueWriterInterface
		:  ChannelWriterInterface, IQueueWriterInterface
	{
		protected const string QueuePrefix = "queue_";

		protected IQueueConnectorWriter attachedQueue;
		public IQueueConnectorWriter AttachedQueue
		{
			get { return attachedQueue; }
			set
			{
		//		if (attachedQueue != null)
		//			throw new Exception("Queue already bound");

				attachedQueue = value;
			}
		}


		public virtual void WriteChannelHierarchy(System.CodeDom.Compiler.IndentedTextWriter writer)
		{
			writer.WriteLine("Queue writer interface '{0}'", LocalName);
		}

		private Module writingDatapath;


		public virtual void BindWriter(IQueueWriterInterface writer)
		{
			throw new NotSupportedException();
		}

		public virtual Module GetWritingDatapath()
		{
			return writingDatapath;
		}

		public void SetWritingDatapath(Module datapath)
		{
			this.writingDatapath = datapath;
		}

		public override List<Module> GetReadingDatapaths()
		{
			return attachedQueue.GetReadingDatapaths();
		}

		public override bool HasAttachedChannel
		{
			get { return attachedQueue != null; }
		}

		public override bool HasAttachedWriter
		{
			get { return false;  }
		}

		#region Constructors
		public QueueWriterInterface(Module parent, QueueConnectorWriterInterface writerInterface, string uniqueName)
			: base(parent, writerInterface.TypeRegistry, writerInterface.Type, writerInterface.LocalName, uniqueName)
		{
			this.attachedQueue = writerInterface;
			this.attachedQueue.BindWriter(this);
		}

		public QueueWriterInterface(Module parent, Queue queue, string uniqueName)
			: base(parent, queue.TypeRegistry, queue.Type, queue.LocalName, uniqueName)
		{
			this.attachedQueue = queue;
			this.attachedQueue.BindWriter(this);
		}

		protected QueueWriterInterface(Module parent, TypeRegistry typeRegistry, Type type, string name, string uniqueName)
			: base(parent, typeRegistry, type, name, uniqueName)
		{
		}
		#endregion
	}
}
